package com.matrix.framework.core.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.matrix.framework.core.common.enums.IEnumDescribable;
import com.matrix.framework.core.common.global.Options;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 通用工具类
 *
 * Copyright © 海平面工作室 版权所有
 *
 * @Author: Leo
 * @Create: 2024/10/3 11:15
 * @Since 1.0
 */

public class Common {

    private static final Logger logger = LoggerFactory.getLogger(Common.class);

    public static <E extends Enum<E>> List<Options> getOptionsList(Class<E> enumClass) {
        List<Options> optionsList = new ArrayList<>();
        E[] enumConstants = enumClass.getEnumConstants();

        for (E enumConstant : enumConstants) {
            String description = ((IEnumDescribable) enumConstant).getDescription();
            optionsList.add(new Options(description, enumConstant.name()));
        }

        return optionsList;
    }

    public static String formatDatetime(long datetime) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(datetime), ZoneId.of("Asia/Shanghai"));
        return dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static <T> T jsonToObject(String json, Class<T> clazz) {
        try {
            return objectMapper.readValue(json, clazz);
        } catch (IOException e) {
            logger.error("fromJson error", e);
            return null;
        }
    }

    public static <T> List<T> jsonToList(String json, Class<T> clazz) {
        try {
            JavaType type = objectMapper.getTypeFactory().constructCollectionType(List.class, clazz);
            return objectMapper.readValue(json, type);
        } catch (IOException e) {
            logger.error("fromJson error", e);
            return null;
        }
    }

    public static Map<String, Object> jsonToMap(String json) {
        try {
            return objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {});
        } catch (IOException e) {
            logger.error("fromJson error", e);
            return null;
        }
    }

    public static <T> String objectToJson(T object) {
        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            logger.error("toJson error", e);
            return null;
        }
    }

    public static <T> String listToJson(List<T> list) {
        try {
            return objectMapper.writeValueAsString(list);
        } catch (JsonProcessingException e) {
            logger.error("toJson error", e);
            return null;
        }
    }

    public static <T> String listToJson(List<T> list, Class<T> clazz) {
        try {
            return objectMapper.writerFor(objectMapper.getTypeFactory().constructCollectionType(List.class, clazz)).writeValueAsString(list);
        } catch (JsonProcessingException e) {
            logger.error("toJson error", e);
            return null;
        }
    }

    public static String mapToJson(Map<String, Object> map) {
        try {
            return objectMapper.writeValueAsString(map);
        } catch (IOException e) {
            logger.error("toJson error", e);
            return null;
        }
    }

    /**
     * 将日期字符串转换为long类型
     * @param s 日期字符串
     * @return 日期字符串对应的long类型
     */
    public static Object DateToLong(String s) {
        return LocalDateTime.parse(s, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).toEpochSecond(ZoneOffset.of("+8"));
    }

}
